<?php
require('system-config.php');

if ( isset($_GET['action']) ) {
	check_admin_referer();
	
	if ('activate' == $_GET['action']) {
		validateToken($_GET['token'], 'systemplugins.php', 'activate', $_GET['plugin']);
		$current = get_settings('active_system_plugins');
		if (!in_array($_GET['plugin'], $current)) {
			$current[] = trim( $_GET['plugin'] );
			sort($current);
			update_option('active_system_plugins', $current);
			include(ABSPATH . 'wp-content/systemplugins/' . trim( $_GET['plugin'] ));
			do_action('activate_' . trim( $_GET['plugin'] ));
		}
		header("Location: systemplugins.php?b=$blogdata->slug&activate=true");
	} else if ('deactivate' == $_GET['action']) {
		validateToken($_GET['token'], 'systemplugins.php', 'deactivate', $_GET['plugin']);
		$current = get_settings('active_system_plugins');
		array_splice($current, array_search( $_GET['plugin'], $current), 1 ); // Array-fu!
		update_option('active_system_plugins', $current);
		do_action('deactivate_' . trim( $_GET['plugin'] ));
		header("Location: systemplugins.php?b=$blogdata->slug&deactivate=true");
	}
}

//$title = __('Manage Plugins');
//require_once(WPADMININC . 'admin-header.php');

// Clean up options
// If any plugins don't exist, axe 'em

$check_plugins = get_settings('active_system_plugins');

// Sanity check.  If the active plugin list is not an array, make it an
// empty array.
if ( !is_array($check_plugins) ) {
	$check_plugins = array();
	update_option('active_system_plugins', $check_plugins);	
}

// If a plugin file does not exist, remove it from the list of active
// plugins.
foreach ($check_plugins as $check_plugin) {
	if (!file_exists(ABSPATH . 'wp-content/systemplugins/' . $check_plugin)) {
			$current = get_settings('active_system_plugins');
			$key = array_search($check_plugin, $current);
			if ( false !== $key && NULL !== $key ) {
				unset($current[$key]);
				update_option('active_system_plugins', $current);
			}
	}
}


// ===========================================================================
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<title><?php bloginfo('name') ?> &rsaquo; <?php echo $title; ?> &#8212; WordPress</title>

	<link rel="stylesheet" href="<?php echo LURL ?>/wp-admin/wp-admin.css" type="text/css" />
	<link rel="stylesheet" href="<?php echo LURL ?>/system-admin/lyceum.css" type="text/css" />

	<meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php echo get_settings('blog_charset'); ?>" />
</head>


<body>

<div id="PageWrapper" class="AdminPlugins">

<?php require(ABSPATH . '/system-admin/lyceum-header.php'); ?>

	<hr />

	<!-- BEGIN: body -->
	<div id="BodyWrapper">

		<!-- BEGIN: main column -->
		<div id="MainColumn">

			<!-- BEGIN: main nav -->
			<ul id="MainNav">
				<li><span><a href="settings.php">System Settings</a></span></li>
				<li><span><a href="blog-management.php">Blogs</a></span></li>
				<li><span><a href="user-management.php">Users</a></span></li>
				<li class="Selected"><span><a href="systemplugins.php">Plugins</a></span></li>
				<li><span><a href="rewriterules.php">Apache Rewrite Rules</a></span></li>
			</ul>
			<div class="ClearFix TabBorder"><!-- do not remove --></div>
			<!-- END: main nav -->


<?php if (isset($_GET['activate'])) : ?>
			<div id="message" class="updated fade">
				<p><?php _e('Plugin <strong>activated</strong>.') ?></p>
			</div>
<?php endif; ?>

<?php if (isset($_GET['deactivate'])) : ?>
			<div id="message" class="updated fade">
				<p><?php _e('Plugin <strong>deactivated</strong>.') ?></p>
			</div>
<?php endif; ?>

			<div class="wrap">
				<h2><?php _e('Plugin Management'); ?></h2>
				<p><?php _e('The plugins listed below are installed in <code>src/lyceum/wp-content/systemplugins</code>.'); ?></p>

<?php
if ( get_settings('active_system_plugins') )
	$current_plugins = get_settings('active_system_plugins');

$plugins = get_plugins(true);

if (empty($plugins)) {
	_e("<p>Couldn't open plugins directory or there are no plugins available.</p>"); // TODO: make more helpful
} else {
?>
				<table width="100%" cellpadding="3" cellspacing="3">
					<tr>
						<th style="text-align: left;"><?php _e('Plugin'); ?> / <?php _e('Description'); ?></th>
						<th><?php _e('Version'); ?></th>
						<th><?php _e('Action'); ?></th>
					</tr>

<?php
	$style = '';

	foreach($plugins as $plugin_file => $plugin_data) {
		$style = ('class="alternate"' == $style|| 'class="alternate active"' == $style) ? '' : 'alternate';

		if (!empty($current_plugins) && in_array($plugin_file, $current_plugins)) {
			$action = "<a href='systemplugins.php?token=".formtoken('systemplugins.php','deactivate',$plugin_file)."&amp;b=$blogdata->slug&amp;action=deactivate&amp;plugin=$plugin_file' title='".__('Deactivate this plugin')."' class='delete'>".__('Deactivate')."</a>";
			$plugin_data['Title'] = "<strong>{$plugin_data['Title']}</strong>";
			$style .= $style == 'alternate' ? ' active' : 'active';
		} else {
			$action = "<a href='systemplugins.php?token=".formtoken('systemplugins.php','activate',$plugin_file)."&amp;b=$blogdata->slug&amp;action=activate&amp;plugin=$plugin_file' title='".__('Activate this plugin')."' class='edit'>".__('Activate')."</a>";
		}
		$plugin_data['Description'] = wp_kses($plugin_data['Description'], array('a' => array('href' => array(),'title' => array()),'abbr' => array('title' => array()),'acronym' => array('title' => array()),'code' => array(),'em' => array(),'strong' => array()) ); ;
		if ($style != '') $style = 'class="' . $style . '"';
?>
					<tr <?php echo $style ?>>
						<td class='name'><strong><?php echo $plugin_data['Title'] ?></strong><br /><?php echo $plugin_data['Description'] ?> <cite>By <?php echo $plugin_data['Author'] ?>.</cite></td>
						<td class='vers'><?php echo $plugin_data['Version'] ?></td>
						<td class='togl'><?php echo $action ?></td>
					</tr>
<?php
	}
?>
				</table>
<?php
}
?>

				<p>If something goes wrong with a plugin, delete or rename that file and it will be automatically deactivated.</p>

			</div>

		</div>
		<!-- END: main column -->

	</div>
	<!-- END: body -->

	<hr />

<?php include(ABSPATH . 'wp-admin/admin-footer.php'); ?>
		
</div>


</body>

</html>
